
import { EventTouch } from 'cc';
import { UIOpacity, UITransform, Vec3 } from 'cc';
import { _decorator, Component, Label, Node, ProgressBar } from 'cc';
import { App } from '../../App';
import UtilTool from '../../tools/UtilTool';
import { Cue } from './Cue';
import { GamePlayState } from './GameBaseLogic';
import { GameBaseModel } from './GameBaseModel';
import { GameModel3 } from './mode3/GameModel3';
const { ccclass, property } = _decorator;

/* 能量条 */
@ccclass('EnergyBar')
export class EnergyBar extends Component {
    @property(Node)
    progressbar: Node = null

    @property(Node)
    cueSpr: Node = null

    private cueCtrl:Cue = null  // 球杆对象
    private _mouseDown = false
    private startUIPos = null   // 开始触摸位置点
    private barHight = 549      // 进度条高度

    protected onLoad(): void {
        
    }

    start() {
        this.progressbar.on(Node.EventType.TOUCH_START, this.touchStartBar, this);
        this.progressbar.on(Node.EventType.TOUCH_MOVE, this.touchMoveBar, this);
        this.progressbar.on(Node.EventType.TOUCH_CANCEL, this.touchCanelBar, this);
        this.progressbar.on(Node.EventType.TOUCH_END, this.touchEndBar, this);

        let gameView = App.Model.gameView as GameBaseModel
        this.cueCtrl = gameView.cueCtrl
        this.resetProgress()
    }

    // 判断是否可以操作能量条
    judgeValid() {
        if (this.cueCtrl==null) {
            return false
        }
        
        //球杆隐藏时操作无效
        if (!this.cueCtrl.node.active || !this.cueCtrl.isMine) {
            return false
        }

        // 摆球阶段操作无效
        let gameView = App.Model.gameView as GameModel3
        if (gameView && gameView.gameLogic &&  gameView.gameLogic.curPlayState == GamePlayState.WAIT_POS_BALL) {
            return false
        } 

        return true
    }

    // 进度条，用来控制出杆力度
    touchStartBar (event: EventTouch) {
        if (!this.judgeValid()) return
        
        // 将按下鼠标的标记设置为true
        this._mouseDown = true

        this.startUIPos = event.getUILocation()
        let nodePos = this.progressbar.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(this.startUIPos.x, this.startUIPos.y, 1))
        let bfb = nodePos.y / this.barHight
        if (bfb<0) bfb = 0
        if (bfb>1) bfb = 1
        this.progressbar.getComponent(ProgressBar).progress = bfb
        this.cueSpr.scale = new Vec3(1,bfb,1)
        //使球杆向后移动 
        this.cueCtrl.moveEneryBarCallBack(1-bfb)
    }

    // 移动力量进度条
    touchMoveBar (event: EventTouch) {
        if (!this.judgeValid()) return
        
        this._mouseDown = true
        let uiPos = event.getUIDelta()
        let progressValue = this.setProgressValue(uiPos.y)

        //使球杆向后移动 
        this.cueCtrl.moveEneryBarCallBack(1-progressValue)
    }

    // 设置进度条的值 0满值 1最小能量
    setProgressValue(v) {
        let disY = v/this.barHight
        let progressValue = this.progressbar.getComponent(ProgressBar).progress + disY
        if (progressValue<0) progressValue = 0
        if (progressValue>1) progressValue = 1
        this.progressbar.getComponent(ProgressBar).progress = progressValue

        this.cueSpr.scale = new Vec3(1,progressValue,1)
        return progressValue
    }

    // 结束力量进度条
    touchEndBar (event: EventTouch) {
        if (!this.judgeValid()) return

        if (!this.cueCtrl.node.active || !this.cueCtrl.isMine) {
            this.resetProgress()
            this.cueCtrl.resetCuePos()
            return;
        }

        if (this.progressbar.getComponent(ProgressBar).progress==1) {
            return
        }
        this.resetProgress()
        this._mouseDown = false
        this.cueCtrl.onMouseUp()
    }

    // 取消力量进度条
    touchCanelBar(event: EventTouch) {
        if (!this.judgeValid()) return

        if (!this.cueCtrl.node.active || !this.cueCtrl.isMine) {
            this.resetProgress()
            this.cueCtrl.resetCuePos()
            return;
        }

        if (this.progressbar.getComponent(ProgressBar).progress==1) {
            return
        }
        this.resetProgress()
        this._mouseDown = false
        this.cueCtrl.onMouseUp()
    }

    // 重置进度条
    resetProgress() {
        this.progressbar.getComponent(ProgressBar).progress = 1
        this.cueSpr.scale = new Vec3(1,1,1)
    }
}

